/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Global Trust Authority is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.7

use crate::key_manager::model::Version;
use sea_orm::entity::prelude::*;
use sea_orm::DatabaseTransaction;
use sea_orm::Statement;
use sea_orm::ConnectionTrait;
use common_log::{error, info};
use rdb::get_connection;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "key_manager_key_version")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub key_version: String,
    pub key_type: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

/// KeyManagerKeyVersion
/// 
/// # Returns
/// 
/// * `Result<Version, DbErr>` - Success or error
/// 
/// # Errors
/// 
/// * `DbErr` - If the database operation fails.
pub async fn get_current_key_version() -> Result<Version, DbErr> {
    info!("KeyManagerKeyVersion: Getting current key version");
    let db = get_connection().await.map_err(|e| {
        error!("Failed to acquire connection from pool: {:?}", e);
        sea_orm::DbErr::Conn(sea_orm::RuntimeErr::Internal("Failed to acquire connection from pool".to_string()))
    })?;
    let db = db.as_ref();
    // let result = Entity::find().one(db).await?;
    let result = Entity::find().all(db).await?.first().cloned();
    result
        .map(|m| Version::new(&m.key_version))
        .ok_or(DbErr::RecordNotFound("No version".into()))
}

/// KeyManagerKeyVersion
///
/// # Arguments
/// 
/// * `new_version` - New version
/// * tx - Transaction
/// 
/// # Returns
/// 
/// * `Result<(), DbErr>` - Success or error
/// 
/// # Errors
/// 
/// * `DbErr` - If the database operation fails.
pub async fn update_key_version(
    new_version: String,
    tx: &DatabaseTransaction,
) -> Result<(), DbErr> {
    info!(
        "KeyManagerKeyVersion: Updating key version: {}",
        new_version
    );
    Entity::delete_many().exec(tx).await?;
    let stmt = Statement::from_sql_and_values(
        tx.get_database_backend(),
        "INSERT INTO key_manager_key_version (key_version, key_type) VALUES (?, ?)",
        vec![
            Value::String(Some(Box::new(new_version))),
            Value::String(Some(Box::new("FILE".to_string()))),
        ],
    );
    tx.execute(stmt).await?;
    Ok(())
}
